package com.youyoulong.framework.controller;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import com.youyoulong.framework.util.SpringContextUtil;
import com.youyoulong.framework.util.ThreadVariableUtil;

@Controller
@RequestMapping(value = "/viewer")
public class ViewController {
	@RequestMapping(value = "/{viewName}")
	public ModelAndView goView(@PathVariable("viewName") String viewName, HttpServletRequest request) {
		ModelMap pageContextAttributes = new ModelMap();
		ModelAndView viever = new ModelAndView(viewName.replace(".", "/"), "pageContext", pageContextAttributes);
		viever.addAllObjects(pageContextAttributes);
		return viever;
	}
	
	@RequestMapping(value = "/setting/language")
	public @ResponseBody String setLanguage(HttpServletRequest request) {
		String language = request.getParameter("language");
		Locale locale = null;
		if (language != null) {
			if (language.equals("zh_CN")) {
				// 汉语-中国
				locale = new Locale("zh", "CN");
			} else if (language.equals("en_US")) {
				// 英语-美国
				locale = new Locale("en", "US");
			} else if (language.equals("ja_JP")) {
				// 日语-日本
				locale = new Locale("ja", "JP");
			} else {
				locale = new Locale("zh", "CN");
			}
		}else{
			locale = Locale.getDefault();
			
		}
		request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);
		request.getSession().setAttribute(ThreadVariableUtil._sessionLanguageKey, locale);
		request.getSession().setAttribute(ThreadVariableUtil._sessionLocaleKey, locale.toString());
		return locale.toString();
	}
	
//	@RequestMapping(value = "/setting/localeMessages", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
//	public @ResponseBody Map<String,String> localeMessages(@RequestBody String[] keys, HttpSession session, HttpServletRequest request)
//			throws Exception {
//		
//		final Map<String,String> result=new HashMap<String,String>();
//		
//		for(final String key:keys){
//			result.put(key, SpringContextUtil.getMessage(key));
//		}
//		
//		return result;
//	}
	
	@RequestMapping(value = "/setting/localeMessages", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Map<String,String> localeMessagesArgs(@RequestBody Map<String,Object[]> messagesWithArgs, HttpSession session, HttpServletRequest request)
			throws Exception {
		
		final Map<String,String> result=new HashMap<String,String>();
		
		for(final String key:messagesWithArgs.keySet()){
			result.put(key, SpringContextUtil.getMessage(key,messagesWithArgs.get(key)));
		}
		
		return result;
	}
	
	

	
}
